<?php
/**
 * Aide d'action qui gère les exports
 * 
 * @package zfbook
 * @subpackage controller
 */
class Zfbook_Controller_ActionHelpers_ExportReservations
extends Zend_Controller_Action_Helper_Abstract
{

    /**
     * Pattern Strategy
     * 
     * @param TReservation $reservations
     * @param string $format (pdf, csv, ...)
     * @return void
     */
    public function direct($format = 'pdf')
    {
        $exportObject = $this->_build($format);
        $this->getActionController()
             ->getResponse()
             ->setHeader('Content-type', 
                         $exportObject->getMimeType());
        $this->getActionController()
             ->getResponse()
             ->setBody($exportObject->getContent());
    }

    /**
     * Construit un objet d'export (builder)
     *
     * @param string $format
     * @return Zfbook_Controller_ActionHelpers_ExportReservations_Interface
     */
    private function _build($format)
    {
        $classToBuild = 'Zfbook_Controller_ActionHelpers_ExportReservations_' . ucfirst($format);
        if (!class_exists($classToBuild)) {
            $msg = "Format de sortie inconnu";
            throw new Zfbook_Controller_Exception($msg);
        }
        $classToBuild = new $classToBuild;
        if (!$classToBuild instanceof 
            Zfbook_Controller_ActionHelpers_ExportReservations_Abstract) {
            $msg = "Classe d'export incorrecte";
            throw new Zfbook_Controller_Exception($msg);
        }
        return new $classToBuild;
    }
}
